import Vue from "vue";
import VueRouter from "vue-router";
import NProgress from "nprogress";
import routes from "./routes";
import $envConfig from "@/utils/envUtils";

Vue.use(VueRouter);

// 解决重复点击同一个路由报错的问题
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

let base = $envConfig("VITE_BASE_PATH") || "";

const createRouter = new VueRouter({
  mode: "history", // hash
  base,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      //如果savedPosition存在，滚动条会自动跳到记录的值的地方
      return savedPosition;
    } else {
      // 异步滚动操作
      return new Promise((resolve) => {
        setTimeout(() => {
          resolve({ x: 0, y: 1 });
        }, 0);
      }); //savedPosition也是一个记录x轴和y轴位置的对象
    }
  },
  routes,
});

NProgress.configure({
  easing: "ease",
  speed: 500,
  trickleSpeed: 200,
  showSpinner: false,
  minimum: 0.3,
});

const router = createRouter;
export default router;
